//
//  CatFeedVController.m
//  DoorBell
//
//  Created by Chun Hung Cheung on 2023/3/17.
//  Copyright © 2023 Chun Hung Cheung. All rights reserved.
//

#import "CatFeedVController.h"

@interface CatFeedVController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property(nonatomic,strong)UIView *contrainView;
@property(nonatomic,strong)UILabel *titleLabel;
@property(nonatomic,strong)UILabel *weightLabel;
@property(nonatomic,strong)UIPickerView *weightPickview;
@property(nonatomic,assign)NSInteger selectedRow;
@property(nonatomic,strong)NSMutableArray *dataArray;
@property(nonatomic,strong)UIButton *feedButon;
@end

@implementation CatFeedVController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.contrainView.backgroundColor = [UIColor whiteColor];
     self.titleLabel.text = NSLocalizedString(@"选择喂食份数",nil);
     //self.weightLabel.text = @"每份重量约10克";
     self.selectedRow = 0;
    [self pickerView:self.weightPickview didSelectRow:self.selectedRow inComponent:0];
    [self.weightPickview reloadAllComponents];
     self.weightPickview.backgroundColor = [UIColor whiteColor];
     self.feedButon.backgroundColor = [UIColor orangeColor];
    
}

- (NSMutableArray*)dataArray
{
    if(!_dataArray){
        _dataArray = [NSMutableArray array];
        for (int i = 1; i < 13; i ++) {
         [_dataArray addObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    return _dataArray;
}


- (UIView*)contrainView
{
    if (!_contrainView) {
         _contrainView = [[UIView alloc]init];
         _contrainView.userInteractionEnabled = YES;
         _contrainView.layer.cornerRadius = 15;
         _contrainView.layer.masksToBounds = YES;
        [self.view addSubview:_contrainView];
        [_contrainView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.view.mas_left);
            make.right.equalTo(self.view.mas_right);
            make.bottom.equalTo(self.view.mas_bottom);
            make.height.mas_equalTo(380);
        }];
    }
    return _contrainView;
}

- (UILabel*)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.textColor = [HexChangeRGB colorWithHexString:@"#333333"];
        _titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:20.0];
        [self.contrainView addSubview:_titleLabel];
        [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.contrainView.mas_centerX);
            make.top.equalTo(self.contrainView.mas_top).offset(17);
        }];
    }
    return _titleLabel;
}

- (UILabel*)weightLabel
{
    if (!_weightLabel) {
         _weightLabel = [[UILabel alloc]init];
         _weightLabel.textAlignment = NSTextAlignmentCenter;
         _weightLabel.textColor = [HexChangeRGB colorWithHexString:@"#666666"];
         _weightLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:13.0];
        [self.contrainView addSubview:_weightLabel];
        [_weightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.contrainView.mas_centerX);
            make.top.equalTo(self.titleLabel.mas_bottom).offset(6);
        }];
    }
    return _weightLabel;
}


- (UIPickerView*)weightPickview
{
    if (!_weightPickview) {
         // 设置UIPickerView
        _weightPickview = [[UIPickerView alloc]init];
        _weightPickview.backgroundColor = [UIColor whiteColor];
        _weightPickview.dataSource = self;
        _weightPickview.layer.borderWidth = 1;
        _weightPickview.layer.borderColor = [UIColor whiteColor].CGColor;
        _weightPickview.delegate = self;
        [self.contrainView addSubview:_weightPickview];
        [_weightPickview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contrainView.mas_right);
            make.left.equalTo(self.contrainView.mas_left);
            make.top.equalTo(self.weightLabel.mas_bottom).offset(5);
            make.height.mas_equalTo(200);
        }];
    }
    return _weightPickview;
}


#pragma mark - UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
    // 组数
    
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{    //行数
    return self.dataArray.count;
    
}
#pragma mark - UIPickerViewDelegate

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 40;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return self.dataArray[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
     NSLog(@"row --%ld",row);
     self.selectedRow = row;
    [self.weightPickview reloadAllComponents];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    //自定义Label设置pickerview展示数据的样式。同理也可以自定义view显示图标+文字。
    NSString *title = self.dataArray[row];
    //self.weightLabel.text = [NSString stringWithFormat:@"每份重量约%@克",title];
    UIView *contrainerView =(UIView *) view;
    if (!contrainerView) {
        contrainerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth-28, 40)];
    }
    
    UILabel  *pickerLabel = [[UILabel alloc]init];
    [pickerLabel sizeToFit];
    pickerLabel.textAlignment = NSTextAlignmentCenter;
    pickerLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:20];
    pickerLabel.textColor = [HexChangeRGB colorWithHexString:@"#333333"];
    [contrainerView addSubview:pickerLabel];
    [pickerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(contrainerView.mas_centerX);
        make.centerY.equalTo(contrainerView.mas_centerY);
    }];

    //调用委托方法，获取需要显示的数据。
    pickerLabel.text = title;
    if (self.selectedRow == row) {
        contrainerView.backgroundColor = [UIColor whiteColor];
    }
    return contrainerView;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (UIButton*)feedButon
{
    if (!_feedButon) {
        _feedButon = [[UIButton alloc]init];
       [_feedButon addTarget:self action:@selector(feedButonClick) forControlEvents:UIControlEventTouchUpInside];
        _feedButon.layer.cornerRadius = 25;
        _feedButon.layer.masksToBounds = YES;
        _feedButon.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:20.0];
       [_feedButon setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
       [_feedButon setTitle:NSLocalizedString(@"投喂", nil) forState:UIControlStateNormal];
       [self.contrainView addSubview:_feedButon];
       [_feedButon mas_makeConstraints:^(MASConstraintMaker *make) {
           make.centerX.equalTo(self.contrainView.mas_centerX);
           make.top.equalTo(self.weightPickview.mas_bottom);
           make.height.mas_equalTo(50);
           make.width.mas_equalTo(280);
       }];
    }
    return _feedButon;
}

- (void)feedButonClick
{
    
    if(self.weightblock){
       self.weightblock(self.selectedRow+1);
    }
    
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
